如何在Matlab中找到文件夹中的.xlsx文件? | 您所在的位置:网站首页 › matlab 查找文件 › 如何在Matlab中找到文件夹中的.xlsx文件? |
我有两个文件夹和一个.xlsx文件project_dir.我想切换到这个文件夹,并将.xlsx文件中的信息读入matlab,但我似乎遇到了问题. 我的代码: project_dir='D:\MPhil\Model_Building\Models\TGFB\Vilar2006\SBML_sh_ver\vilar2006_SBSH_test7\Python_project3_IQM_project\vilar2006_SBSH_test6_2'; cd(project_dir) dir *.xlsx这项工作是因为它没有给我一个xlsx文件的句柄供后续阅读. dir(project_dir)也不起作用,因为你得到了一个xlsread函数无法读取的结构. 有人知道怎么做这个吗? 1> rayryeng - R..: dir返回结构数组的地方是正确的.此数组中的每个元素都是有关使用时与搜索查询匹配的每个文件的信息dir.你显然不能直接使用这个结构,所以你必须访问它里面的东西才能得到你需要的东西.首先,您需要将输出分配dir给某些内容: d = dir('*.xlsx');在此之后,您将获得一个结构数组,其中每个元素都有一个name字段.此字段是与您的查询匹配的每个文件的名称dir.因此,您必须name单独访问每个文件的每个字段,并使用它来最终打开您的文件. 例如,如果您想要第一个Excel文件,您可以: fileName = d(1).name;fileName 将包含第一个Excel文件的名称的字符串,然后您可以使用它来读取文件: out = xlsread(fileName);或者,如果您有多个Excel文件并且想要单独处理它们,请考虑将其置于循环中: d = dir('*.xlsx'); %// Find all Excel files %// For each file... for idx = 1 : numel(d) fileName = d(idx).name; %// Get the file name out = xlsread(fileName); %// Read the Excel file %//..... rest of your code follows end如果您想要查找不在当前工作目录中的Excel文件,则可以执行以下操作.请记住,在使用时dir,它会查找与您指定的输入目录相关的文件.这不构成绝对路径.因此,要成功打开文件,您需要将指定的目录以及该目录的本地相关文件名拼凑在一起.您可以用它fullfile来帮助您: directory = '/put/my/directory/here'; %// Place directory to search for Excel files here %// Create absolute path to search for Excel files searchString = fullfile(directory, '*.xlsx'); %// Find the file names d = dir(searchString); %// For each file... for idx = 1 : numel(d) fileName = fullfile(directory, d(idx).name); %// Get the file name out = xlsread(fileName); %// Read the Excel file %//..... rest of your code follows end如果您正在当前工作目录中查找文件,那么这些fullfile内容不是必需的......如果您要搜索不在工作目录中的Excel文件,可以考虑一下. |
CopyRight 2018-2019 实验室设备网 版权所有 |